﻿using System;
using System.Collections.Generic;
using FluffyUnderware.DevTools.Extensions;
using Gameplay.PVE;
using TMPro;
using UnityEngine;
using Yoozoo.Gta.Common;
using Yoozoo.Managers.ResourceManagerV2.Runtime;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.City
{
    public class AnimationCallBack
    {
        public float time;
        public Action callBack;
        public AnimationCallBack(float _time, Action _callBack)
        {
            time = _time;
            callBack = _callBack;
        }
        
        public void playCallBack()
        {
            if (callBack != null)
                callBack?.Invoke();
        }
    }
    public class CityWallGangBoard : MonoBehaviour
    {
        //public SpriteRenderer gangLogoImage;
        public List<GameObject> gangLogoList;
        //public SpriteRenderer gangLogoLeftImage;
        public List<GameObject> gangLogoLeftList;
        public YImage rootLevelImage;
        public TextMeshPro gangNameText;
        public YImage[] starImageArr; 
        public GameObject leftAnimEmpty; 
        public GameObject leftAnimLogo; 
        public GameObject rightAnimEmpty; 
        public GameObject rightAnimLogo;
        private ResLoader _resLoader;
        private List<AnimationCallBack> _arrayAnimationCallBack;
        private float time;
        private string _currentLogoPath;
        private string _currentRootLevelPath;
        private string _currentStarLevelPath;
        private void Start()
        {
            _arrayAnimationCallBack = new List<AnimationCallBack>();
            UpdateGang();
            UpdateWallStar();
            CityManager.OnUpdateGangSetting += UpdateGang;
            CityManager.OnUpdateTempGangSetting += UpdateTempGang;
            CityManager.OnUpdateWallStar += UpdateWallStar;
            CityManager.OnUpdateWallStarSetting += UpdateWallStarSet;
        }

        private void Update()
        {
            time += Time.deltaTime;
            if (_arrayAnimationCallBack.Count > 0)
            {
                for (int i = _arrayAnimationCallBack.Count - 1; i >= 0; i--)
                {
                    if (time >= _arrayAnimationCallBack[i].time)
                    {
                        _arrayAnimationCallBack[i].playCallBack();
                        _arrayAnimationCallBack.Remove(_arrayAnimationCallBack[i]);
                    }
                }
            }
        }

        private void UpdateWallStar()
        {
            int rootLevel = ResidentHandler.Inst.GetFunction("CityLuaFunction.getBusinessUnLockMaxGradeRootLevel").Func<int,int>(0);
            int starLevel = ResidentHandler.Inst.GetFunction("CityLuaFunction.getBusinessUnLockMaxGradeStarLevel").Func<int,int>(0);
            UpdateWallStarSetting(rootLevel, starLevel);
        }
        
        private void UpdateWallStarSet(int rootLevel , int starLevel)
        {
            UpdateWallStarSetting(rootLevel, starLevel);
        }
        
        private void UpdateWallStarSetting(int rootLevel, int starLevel)
        {
            
            _currentRootLevelPath = "Assets/ResourcesAssets/UI/GUI/GangLogo/door_bg_" + rootLevel + ".png";
            _currentStarLevelPath = "Assets/ResourcesAssets/UI/GUI/GangLogo/door_star_" + rootLevel + ".png";
            _resLoader = ResLoader.Alloc();
            _resLoader.Add2Load(_currentRootLevelPath, typeof(Sprite), ((success, assetName, asset) =>
            {
                if (!success)
                {
                    return;
                }
            
                if (assetName != _currentRootLevelPath)
                {
                    return;
                }
                
                rootLevelImage.sprite = asset as Sprite;
            }));
            _resLoader.Load();
            
            _resLoader.Add2Load(_currentStarLevelPath, typeof(Sprite), ((success, assetName, asset) =>
            {
                if (!success)
                {
                    return;
                }
            
                if (assetName != _currentStarLevelPath)
                {
                    return;
                }
                for (int i = 1; i < 6; i++)
                {
                    starImageArr[i-1].gameObject.SetActive(i <= starLevel);
                    if (i <= starLevel)
                    {
                        starImageArr[i-1].sprite = asset as Sprite;
                    }
                }
            }));
            _resLoader.Load();
        }

        private void UpdateGang()
        {
            int logoId = ResidentHandler.Inst.GetFunction("CityLuaFunction.getGangLogoId").Func<int,int>(0);
            string gangName = ResidentHandler.Inst.GetFunction("CityLuaFunction.getGangName").Func<int,string>(0);

            UpdateGangSetting(logoId - 1,gangName);
        }

        private void UpdateTempGang(int logoIndex, string gangName)
        {
            UpdateGangSetting(logoIndex,gangName);
        }
        
        private void UpdateGangSetting(int logoIndex, string gangName)
        {
            if (logoIndex >= 0)
            {
                for (int i = 0; i < gangLogoList.Count; i++)
                {
                    gangLogoList[i].SetActive(false);
                    if (i == logoIndex)
                    {
                        gangLogoList[i].SetActive(true);
                    }
                }
                for (int i = 0; i < gangLogoLeftList.Count; i++)
                {
                    gangLogoLeftList[i].SetActive(false);
                    if (i == logoIndex)
                    {
                        gangLogoLeftList[i].SetActive(true);
                    }
                }
            
                leftAnimEmpty.SetActive(false);
                rightAnimEmpty.SetActive(false);
                leftAnimLogo.SetActive(true);  
                rightAnimLogo.SetActive(true);
                playAnim(leftAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_once", ()=>{
                    playAnim(leftAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_keep02");
                });
                playAnim(rightAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_once", ()=>{
                    playAnim(rightAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_keep02");
                });
                
            }
            else
            {
                for (int i = 0; i < gangLogoList.Count; i++)
                {
                    gangLogoList[i].SetActive(false);
                }
                for (int i = 0; i < gangLogoLeftList.Count; i++)
                {
                    gangLogoLeftList[i].SetActive(false);
                }
            }
            
            // if (!string.IsNullOrEmpty(logoName))
            // {
            //     _currentLogoPath = "Assets/ResourcesAssets/UI/GUI/GangLogoLight/" + logoName + ".png";
            //     _resLoader = ResLoader.Alloc();
            //     _resLoader.Add2Load(_currentLogoPath, typeof(Sprite), ((success, assetName, asset) =>
            //     {
            //         if (!success)
            //         {
            //             return;
            //         }
            //
            //         if (assetName != _currentLogoPath)
            //         {
            //             return;
            //         }
            //         gangLogoImage.sprite = asset as Sprite;
            //         gangLogoLeftImage.sprite = asset as Sprite;
            //         leftAnimEmpty.SetActive(false);
            //         rightAnimEmpty.SetActive(false);
            //         leftAnimLogo.SetActive(true);  
            //         rightAnimLogo.SetActive(true);
            //         playAnim(leftAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_once", ()=>{
            //             playAnim(leftAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_keep02");
            //         });
            //         playAnim(rightAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_once", ()=>{
            //             playAnim(rightAnimLogo.transform.GetComponent<Animation>(), "ami_sfx_ui_damen_keep02");
            //         });
            //         playAnim(gangLogoImage.transform.GetComponent<Animation>(), "ami_InnerCity_61004_Image");
            //         playAnim(gangLogoImage.transform.GetComponent<Animation>(), "ami_InnerCity_61004_Image");
            //     }));
            //     _resLoader.Load();
            // }
            // else
            // {
            //     leftAnimEmpty.SetActive(true);
            //     rightAnimEmpty.SetActive(true);
            //     leftAnimLogo.SetActive(false);  
            //     rightAnimLogo.SetActive(false);
            //     gangLogoImage.sprite = null;
            //     gangLogoLeftImage.sprite = null;
            // }
            gangNameText.text = gangName;
        }

        private void playAnim(Animation animation, string animationName, Action callback = null)
        {
            if (animation == null)
            {
                if (callback != null)
                    callback?.Invoke();
                return;
            }

            AnimationClip animationClip = animation.GetClip(animationName);
            var clipLength = animationClip.length;
            if (animationClip == null)
            {
                if (callback != null)
                    callback?.Invoke();
                return;
            }
            animation.Play(animationName);
            if (callback != null)
            {
                AnimationCallBack animationCallBack = new AnimationCallBack(time + clipLength, callback);
                _arrayAnimationCallBack.Add(animationCallBack);
            }
        }

        private void OnDestroy()
        {
            CityManager.OnUpdateGangSetting -= UpdateGang;
            CityManager.OnUpdateTempGangSetting -= UpdateTempGang;
            CityManager.OnUpdateWallStar -= UpdateWallStar;
            CityManager.OnUpdateWallStarSetting -= UpdateWallStarSet;
            time = 0;
            _arrayAnimationCallBack.Clear();
            if (_resLoader!=null)
            {
                _resLoader.Recycle2Cache();
                _resLoader = null;
            }
        }

       
    }
}